package dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.User;

public class UserMod {
	Connection connection;
	private static UserMod isntance;

	public static UserMod getIsntance() {
		if (isntance == null) {
			isntance = new UserMod();
		}
		return isntance;
	}

	public boolean isUID(int uid) throws IOException { /*
														 * kiem tra tinh hop le
														 * cua nid va co nam
														 * trong DB hay khong
														 */
		connection = DB_Connection.getIsntance().getConnection();
		PreparedStatement sqlp = null;
		boolean result = false;
		try {
			sqlp = connection
					.prepareStatement("SELECT uid FROM users where uid = ? ");
			sqlp.setInt(1, uid);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next()) {
				result = true;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();

		}
		return result;
	}

	public boolean addUser(User user) {
		Connection connection = DB_Connection.getIsntance().getConnection();
		PreparedStatement sqlr = null;
		boolean result = false;
		try {
			sqlr = connection
					.prepareStatement("INSERT INTO users(user, pass, level) VALUES (?, ?, ?)");
			sqlr.setString(1, user.getUser());
			sqlr.setString(2, user.getPass());
			sqlr.setInt(3, user.getLevel());
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public User checkLogin(String u, String p) {
		PreparedStatement sqll = null;
		User result = null;
		Connection connection = DB_Connection.getIsntance().getConnection();
		try {
			sqll = connection
					.prepareStatement("select uid, user , pass , level  from users where user = ? AND pass = ?");
			sqll.setString(1, u);
			sqll.setString(2, p);
			ResultSet rs = sqll.executeQuery();

			while (rs.next()) {
				int id = rs.getInt(1);
				String user_name = rs.getString(2);
				String pass = rs.getString(3);
				int l = rs.getInt(4);
				result = new User(id, user_name, pass, l);

			}
			sqll.close();
			connection.close();
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return result;
	}

	public boolean del(int uid) {
		if (TeachersMod.getIsntance().isExitUidInTable(uid)) {// kiem tra co ton
																// tai trong
																// bang teacher
			if (!TeachersMod.getIsntance().delByUid(uid))
				return false; // neu co thi del
		} else {
			if (ParentsMod.getIsntance().isExitUidInTable(uid)) { // kiem tra co
																	// ton tai
																	// trong ban
																	// parent
																	// hay ko
				if (!ParentsMod.getIsntance().delByUid(uid))
					return false; // neu co thi del
			}
		}
		boolean result = false;
		PreparedStatement delpd = null;
		Connection connection = DB_Connection.getIsntance().getConnection();// get
																			// connection
		try {
			delpd = connection
					.prepareStatement("DELETE FROM users WHERE uid = ? LIMIT 1");
			delpd.setInt(1, uid);
			int number = delpd.executeUpdate();
			if (number > 0) {
				result = true;
			}
			delpd.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();

		}
		return result;
	}

	public boolean upDateLV(int uid, int lv) {
		Connection connection = DB_Connection.getIsntance().getConnection();
		PreparedStatement sqlr = null;
		boolean result = false;
		try {
			sqlr = connection
					.prepareStatement("UPDATE users SET level = ? WHERE uid = ? LIMIT 1");
			sqlr.setInt(1, lv);
			sqlr.setInt(2, uid);
			int number = sqlr.executeUpdate();
			if (number > 0) {
				result = true;
			}
			sqlr.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean changePass(String pass, String user, String newPass) {
		boolean result = false;
		try {

			connection = DB_Connection.getIsntance().getConnection();
			String sql = "UPDATE users Set pass = (?) Where user =(?)And pass =(?)";
			PreparedStatement preparedStatement = connection
					.prepareStatement(sql);
			preparedStatement.setString(1, newPass);
			preparedStatement.setString(2, user);
			preparedStatement.setString(3, pass);
			int i = preparedStatement.executeUpdate();
			if (i > 0)
				result = true;
			System.out.println(i);
			preparedStatement.close();
			connection.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public ArrayList<User> getAll() {
		ArrayList<User> result = null;
		PreparedStatement sqls = null;
		try {
			connection = DB_Connection.getIsntance().getConnection();
			String sql = "SELECT uid , user , pass , level FROM users";
			sqls = connection.prepareStatement(sql);
			ResultSet rs = sqls.executeQuery();
			while (rs.next()) {
				result.add(new User(rs.getInt(1), rs.getString(2), rs
						.getString(3), rs.getInt(4)));
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static void main(String[] args) {

	}
}
